<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getShops">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="shops" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="店铺名" width="120" sortable>
			</el-table-column>
      <el-table-column prop="state" label="状态" width="120" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1" style="color: red">待审核</span>
          <span v-if="scope.row.state == 2" style="color: #13ce66">审核通过,待激活</span>
          <span v-if="scope.row.state == 3" style="color: green">激活成功</span>
          <span v-if="scope.row.state == 4" style="color: red">驳回</span>
        </template>
			</el-table-column>
      <el-table-column prop="address" label="地址" width="180" sortable>
      </el-table-column>
      <el-table-column prop="admin.username" label="店长" width="120" sortable>
      </el-table-column>
      <el-table-column prop="registerTime" label="创建时间" width="120" sortable>
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button type="danger" size="small" @click="getAuite(scope.$index, scope.row)">审核</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination 
          layout="prev, pager, next" 
          :current-page="query.page"
          @current-change="handleCurrentChange" 
          :page-size="query.pageSize" 
          :total="query.total" 
          style="float:right;">
			</el-pagination>
		</el-col>
		<!--新增/修改界面-->
    <el-dialog title="新增/修改" :visible.sync="shopFormVisible" :close-on-click-modal="false">
      <el-form :model="addShop" label-width="80px" :rules="shopFormRules" ref="addShop">
        <el-form-item label="店名" prop="sn">
          <el-input v-model="addShop.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="tel">
          <el-input  v-model="addShop.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="addShop.address" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="addShop.state">
            <el-radio class="radio" :label="2">驳回</el-radio>
            <el-radio class="radio" :label="1">审核通过</el-radio>
            <el-radio class="radio" :label="0">待审核</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="管理人员">
          <el-select v-model="addShop.employee" clearable value-key="id" placeholder="请选择">
            <el-option
                v-for="item in employees"
                :key="item.id"
                :label="item.username"
                :value="item">
              <span style="float: left">{{ item.username }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="shopFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
    <el-dialog title="审核" :visible.sync="shopAuVisible" :close-on-click-modal="false">
      <el-form :model="shopRusouce" label-width="80px" :rules="shopRules" ref="addShop">
        <el-form-item label="键" prop="key">
          <el-input v-model="shopRusouce.key" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input  v-model="shopRusouce.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="说明">
          <el-input v-model="shopRusouce.intro" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="shopGo"  :loading="addLoading">通过</el-button>
        <el-button type="primary" @click.native="shopBack">驳回</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
			query:{
				total: 0,
				page: 1,
        keyword: '',
        pageSize:10
      },
        shopRusouce:{
			    key:"",
          value:"",
          intro:''
        },
        department:[],
				shops: [],
				listLoading: false,
				sels: [],//列表选中列
        employees:[],
				shopFormVisible: false,//新增界面是否显示
				shopAuVisible: false,//新增界面是否显示
				addLoading: false,
				shopFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
        shopRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增/修改界面数据
				addShop: {
				  id:null,
          name:null,
          tel:'',
          registerTime:'',
          state:'',
          address:'',
          logo:'',
          admin:{
            id:null,
            username:''
          }
				}
			}
		},
		methods: {
		  //审核驳回
      shopBack:function (){
        this.$http.post("/shop/nopass",this.shopRusouce)
            .then(result=>{
              result = result.data;
              if(result.success){
                this.$message({ message: '驳回成功', type: 'success' });
                this.shopAuVisible = false;
                this.getShops();
              }
            }).catch(result=>{
          this.$message({ message: '驳回失败', type: 'success'+result.message });
        })
      },
		  //审核通过
      shopGo: function(){
        this.$http.post("/shop/pass",this.shopRusouce)
                  .then(result=>{
                    result = result.data;
                    if(result.success){
                      this.$message({ message: '审核成功', type: 'success' });
                      this.shopAuVisible = false;
                      this.getShops();
                    }
                  }).catch(result=>{
          this.$message({ message: '审核失败', type: 'success'+result.message });
        })
      },
		  //审核店铺
      getAuite:function (index, row) {
        this.shopAuVisible = true;
        this.addShop = Object.assign({}, row);
        this.shopRusouce.key = this.addShop.id;
        this.shopRusouce.value = this.addShop.admin.id;
      },
		  //获取部门列表
      getDepartment(){
        this.$http.get("/department/list")
                  .then(result =>{
                    result = result.data;
                    if(result.success){
                      this.department = result.result
                      console.log(this.department)
                    }
                  })
      },
      //获取当前行数据
      handleCurrentChange(val) {
				this.query.page = val;
				this.getShops();
			},
      //获取用户列表
      getEmployees() {
        this.listLoading = true;
        //NProgress.start();
        this.$http.post("/employee",this.query)
            .then((res) => {
              res = res.data;
              console.log(res);
              if(res.success){
                this.employees = res.result.datas;
                this.query.total = res.result.total;
              }
              this.listLoading = false;
            });
      },
			//获取店铺列表
			getShops() {
				this.listLoading = true;
				//NProgress.start();
			this.$http.post("/shop",this.query)
                .then((res) => {
                  res = res.data;
                  console.log(res);
                  if(res.success){

                    this.shops = res.result.datas;
                    this.query.total = res.result.total;
                  }
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
          // debugger
          this.$http.delete("/shop/"+row.id)
                    .then((res) => {
                      // console.log("id:"+row.id)
						        this.listLoading = false;
                      if(res.data.success){
						            this.$message({ message: '删除成功', type: 'success' });
                      //删除以后重新从第一页开始查询
						        this.getShops();
                      }else{
                        this.$message({ message: '删除失败'+res.data.message, type: 'error' });
                      }
					});
				}).catch(() => {
          this.$message({ message: '已取消删除', type: 'info' });
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.shopFormVisible = true;
        this.getDepartment();
				this.addShop = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.shopFormVisible = true;
				this.addShop = {
          id:null,
          name:null,
          tel:'',
          registerTime:'',
          state:'',
          address:'',
          logo:'',
          admin:{
            id:null,
            username:''
          }
        };
        this.getEmployees();
			},
			//新增
			save: function () {
        // debugger
				this.$refs.addShop.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addShop);

							console.log("id:"+para.id)
						this.$http.put("/shop",para)
                      .then((res) => {
								      this.addLoading = false;
                        if(res.data.success){
                          this.$message({ message: '保存成功', type: 'success' });
                          this.shopFormVisible = false;
                          this.query.page = 1;
                          this.getShops();
                        }else{
                          this.$message({ message: '保存失败'+res.data.message, type: 'success' });
                        }
							});
						}).catch(() => {
              this.$message({ message: '已取消新增操作', type: 'info' });
            });
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					this.$http.patch("/shop",ids)
                    .then((res) => {
						        this.listLoading = false;
						        if(res.data.success){
                    this.$message({ message: '删除成功', type: 'success' });

                    }else{
                    this.$message({ message: '删除失败'+res.data.message, type: 'error' });

                    }
                    this.query.page = 1;
                    this.getShops();
					});
				}).catch(() => {
          this.$message({ message: '已取消批量删除', type: 'info' });
				});
			}
		},
		mounted() {
			this.getShops();
		}
	}

</script>

<style scoped>

</style>